package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.base.Result;
import com.lin.entity.Device;
import com.lin.entity.Line;
import com.lin.entity.LineDto;
import com.lin.mapper.DeviceMapper;
import com.lin.service.LineService;
import com.lin.mapper.LineMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author admin
* @description 针对表【line】的数据库操作Service实现
* @createDate 2023-04-23 17:25:18
*/
@Service
public class LineServiceImpl extends ServiceImpl<LineMapper, Line>
    implements LineService{

    @Resource
    private DeviceMapper deviceMapper;
    @Override
    public Result getData(Integer departmentDid, String date) throws Exception {

//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String startTime = simpleDateFormat.format(date);
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(startTime);
//        calendar.add(Calendar.HOUR_OF_DAY, 23);
//        calendar.add(Calendar.MINUTE, 59);
//        calendar.add(Calendar.SECOND, 59);
//        Date endTime = calendar.getTime();



        List<LineDto> dtoList = new ArrayList<>();
        List<Device> deviceList = deviceMapper.selectList(new LambdaQueryWrapper<Device>().eq(Device::getDepartmentDid, departmentDid));

        for (Device device : deviceList) {
            LineDto lineDto = new LineDto();
            List<Line> list = list(new LambdaQueryWrapper<Line>()
                    .eq(Line::getDeviceId, device.getId())
                    .like(Line::getTime,date));
            List<Integer> value = list.stream().map(Line::getValue).collect(Collectors.toList());
            lineDto.setName(device.getName());
            lineDto.setType("line");
            lineDto.setData(value);
            dtoList.add(lineDto);
        }

        List<String> nameList = deviceList.stream().map(Device::getName).collect(Collectors.toList());
        Map map = new HashMap<>();
        map.put("dtoList",dtoList);
        map.put("nameList", nameList);

        return Result.ok(map);
    }
}




